const cloud = require('wx-server-sdk');

cloud.init({
  env: cloud.DYNAMIC_CURRENT_ENV
});
const db = cloud.database();
const _ = db.command;

exports.main = async (event, context) => {

  const wxContext = cloud.getWXContext()
  
  const user = await db.collection("user").where({
    openid:wxContext.OPENID,
  }).get()
console.log(user)
  if(user.data.length == 0)
  {
    return {
      code:401,
      data:{},
      msg:"have not login"
   }
  }
  
  if(user.data[0].status != 0 && user.data[0].status != 1)
  {
    return {
      code:401,
		 data:{},
		 msg:"no privilege"
    }
  }
  //获取六小时前的时间
  let timestamp = new Date(new Date().valueOf() -  21600000).valueOf()

  const resul = await db.collection("order").where({
    state:3,
    sid:user.data[0].sid,
    //获取六小时前到现在的未审核预约记录
    timestamp:_.gt(timestamp)
  }).field({
    _id:true,
    timestamp:true,
    sid:true,
    name:true,
    judge_person:true,
    reserve_reason:true
  }).get()

  if(resul.data.length != 0)
  {
    return {
      code:200,
      data:resul.data,
      msg:"请先完成之前的预约"
    }
  }
  return {
    code:404,
    dat:{},
    msg:"没有待审核的预约"
  }
  


  
};
